package com.soriya.nestlive.im;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelRepository {

    private static final Map<Long, ChannelGroup> groupChannel = new ConcurrentHashMap<>();

    public static void addGroup(Long groupId, Channel channel) {
        ChannelGroup channels = groupChannel.get(groupId);
        if (channels == null) {
            channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
            channels.add(channel);
            groupChannel.put(groupId, channels);
            return;
        }
        channels.add(channel);
    }

    public static void removeGroup(Long groupId, Channel channel) {
        ChannelGroup channels = groupChannel.get(groupId);
        if (channels != null) {
            channels.remove(channel);
        }
    }

    public static ChannelGroup getChannelGroup(Long groupId) {
        return groupChannel.get(groupId);
    }

}
